//
//  HSSFHomePageViewController.m
//  HSSFinancial
//
//  Created by xinqiao on 2017/1/10.
//  Copyright © 2017年 Jon_G. All rights reserved.
//

#import "HSSFHomePageViewController.h"


#import "HomePageHeaderView.h"
#import "HomeHuiPlanTableViewCell.h"
#import "HomeFlexibleTableViewCell.h"
#import "MBProgressHUD.h"
#import "G_Color.h"

#import "HuisangsuoApi.h"
#import "promptAlertViewController.h"

@interface HSSFHomePageViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) MBProgressHUD *hud;
@property (nonatomic, strong) HomePageHeaderView *homeheaderView;
@property (nonatomic, strong) NSDictionary *homePageDic;

@end

@implementation HSSFHomePageViewController

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = YES;
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.tintColor = [G_Color colorWithNSString:@"333333" alpha:1];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [G_Color colorWithNSString:@"333333" alpha:1]};
    self.edgesForExtendedLayout = UIRectEdgeBottom | UIRectEdgeLeft | UIRectEdgeRight;
    self.tabBarController.tabBar.hidden=NO;
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self getHomePageDate];
    [self getHomePageDataSourceToSetUI];
}
// 请求
- (void)getHomePageDate {
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.mode = MBProgressHUDModeCustomView;
    self.hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    self.hud.bezelView.color = [UIColor clearColor];
    [HuisangsuoApi requestgetDate:MainIndex dic:nil AndBack:^(id obj) {
        [self.hud hideAnimated:YES];
        if (obj != nil) {
            if ([obj[@"status"] integerValue] == 1) {
                
                self.homeheaderView.imageArray = obj[@"imglist"];
                self.homeheaderView.leiJiLabel.text = [NSString stringWithFormat:@"%@",obj[@"indexdata"][@"chengjiao_money"]];
                self.homeheaderView.zhuCeLabel.text = [NSString stringWithFormat:@"%@",obj[@"indexdata"][@"members_total"]];
                self.homeheaderView.touZiLabel.text = [NSString stringWithFormat:@"%@",obj[@"indexdata"][@"chengjiao_shouyi"]];
                self.homeheaderView.announcementArray = obj[@"gglist"];
                self.homePageDic = obj;
                [self.homePageTableView reloadData];
            }else {
                [promptAlertViewController showButtonAlertView:self withMessage:obj[@"message"] actionMessage:@"确定"];
            }
        }
    } error:^(id obj) {
        [self.hud hideAnimated:YES];
        [promptAlertViewController showButtonAlertView:self withMessage:@"请求错误，稍后重试！" actionMessage:@"确定"];
    }];
}
#pragma mark - UITableView
- (void)getHomePageDataSourceToSetUI {
    self.homePageTableView.delegate = self;
    self.homePageTableView.dataSource = self;
    self.homeheaderView = [[HomePageHeaderView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 322)];
    self.homePageTableView.tableHeaderView = self.homeheaderView;
    self.homePageTableView.tableHeaderView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 218);
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 3) {
        return 10;
    }
    return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 201;
    }
    return 121;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (indexPath.section == 0) {
        HomeHuiPlanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"HomeHuiPlanTableViewCell" owner:self options:nil]lastObject];
            // A
            cell.nameA.text = [NSString stringWithFormat:@"%@",self.homePageDic[@"funddata"][0][@"name"]];
            cell.jilvA.text = [NSString stringWithFormat:@"%@%%",self.homePageDic[@"funddata"][0][@"data"][@"borrow_basis_rate"]];
            cell.qiXianA.text = [NSString stringWithFormat:@"%@",self.homePageDic[@"funddata"][0][@"data"][@"borrow_duration"]];
            if ([self.homePageDic[@"funddata"][0][@"data"][@"is_full"] integerValue] == 0) {
                cell.manBiaoImageViewA.hidden = YES;
            }
            if ([self.homePageDic[@"funddata"][0][@"data"][@"is_full"] integerValue] == 1) {
                cell.buttonA.backgroundColor = [UIColor whiteColor];
                [cell.buttonA setTitle:@"查看详情" forState:UIControlStateNormal];
                [cell.buttonA setTitleColor:[G_Color colorWithNSString:@"ff5050" alpha:1.0] forState:UIControlStateNormal];
            }
            
            // B
            cell.nameB.text = [NSString stringWithFormat:@"%@",self.homePageDic[@"funddata"][1][@"name"]];
            cell.jiLvB.text = [NSString stringWithFormat:@"%@%%",self.homePageDic[@"funddata"][1][@"data"][@"borrow_basis_rate"]];
            cell.qiXianB.text = [NSString stringWithFormat:@"%@",self.homePageDic[@"funddata"][1][@"data"][@"borrow_duration"]];
            if ([self.homePageDic[@"funddata"][1][@"data"][@"is_full"] integerValue] == 0) {
                cell.manBiaoImageViewB.hidden = YES;
            }
            if ([self.homePageDic[@"funddata"][1][@"data"][@"is_full"] integerValue] == 1) {
                cell.buttonB.backgroundColor = [UIColor whiteColor];
                [cell.buttonB setTitle:@"查看详情" forState:UIControlStateNormal];
                [cell.buttonB setTitleColor:[G_Color colorWithNSString:@"F7BC2A" alpha:1.0] forState:UIControlStateNormal];
            }
            // C
            cell.nameC.text = [NSString stringWithFormat:@"%@",self.homePageDic[@"funddata"][2][@"name"]];
            cell.jiLvC.text = [NSString stringWithFormat:@"%@%%",self.homePageDic[@"funddata"][2][@"data"][@"borrow_basis_rate"]];
            cell.qiXianC.text = [NSString stringWithFormat:@"%@",self.homePageDic[@"funddata"][2][@"data"][@"borrow_duration"]];
            if ([self.homePageDic[@"funddata"][2][@"data"][@"is_full"] integerValue] == 0) {
                cell.manBiaoC.hidden = YES;
            }
            if ([self.homePageDic[@"funddata"][2][@"data"][@"is_full"] integerValue] == 1) {
                cell.buttonC.backgroundColor = [UIColor whiteColor];
                [cell.buttonC setTitle:@"查看详情" forState:UIControlStateNormal];
                [cell.buttonC setTitleColor:[G_Color colorWithNSString:@"58AEFF" alpha:1.0] forState:UIControlStateNormal];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell setAccessoryType:UITableViewCellAccessoryNone];
            return cell;
        }
    }else {
        HomeFlexibleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"HomeFlexibleTableViewCell" owner:self options:nil]lastObject];
            
            if (indexPath.section == 1) {
                cell.nameLabel.text = @"汇分期投资";
                cell.shouYiLabel.text = [NSString stringWithFormat:@"%@-%@%%",self.homePageDic[@"ratedata"][@"fq_min_rate"],self.homePageDic[@"ratedata"][@"fq_max_rate"]];
                cell.flexibleImageView.image = [UIImage imageNamed:@"home_Hui"];
            }
            if (indexPath.section == 2) {
                cell.nameLabel.text = @"农户借贷宝";
                cell.shouYiLabel.text = [NSString stringWithFormat:@"%@-%@%%",self.homePageDic[@"ratedata"][@"nh_min_rate"],self.homePageDic[@"ratedata"][@"nh_max_rate"]];
                cell.flexibleImageView.image = [UIImage imageNamed:@"home_Nong"];
            }
            if (indexPath.section == 3) {
                cell.nameLabel.text = @"短期借贷宝";
                cell.shouYiLabel.text = [NSString stringWithFormat:@"%@-%@%%",self.homePageDic[@"ratedata"][@"dq_min_rate"],self.homePageDic[@"ratedata"][@"dq_max_rate"]];
                cell.flexibleImageView.image = [UIImage imageNamed:@"home_Duan"];
            }
            
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell setAccessoryType:UITableViewCellAccessoryNone];
            return cell;
        }
    }
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
